﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

using Microsoft.Xna.Framework;

using SpaceAdventureLib.Universe;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceAdventureLib.Universe.Weapons
{
    [Serializable]
    public abstract class Ammo : Collidable
    {

        #region "Properties"

        private Color color = Color.White;
        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        private float lifespan = float.MaxValue;
        public float LifeSpan
        {
            get { return this.lifespan; }
            set { this.lifespan = value; }
        }

        private float explodingSpan = float.MaxValue;
        public float ExplodingLifeSpan
        {
            get { return this.explodingSpan; }
            set { this.explodingSpan = value; }
        }

        private bool isActive = false;
        public bool IsActive
        {
            get { return this.isActive; }
        }

        private bool isExploding = false;
        public bool IsExploding
        {
            get { return this.isExploding; }
        }

        private bool isUsed = false;
        public bool IsDestroyed
        {
            get { return (this.isUsed && !this.isActive && !this.isExploding); }
        }

        private float damage = 0;
        public float Damage
        {
            get { return this.damage; }
            set { this.damage = value; }
        }

        #endregion

        #region "Variables"

        private Space space = null;
        private float counter = 0;

        #endregion

        #region "Abstract Methods"

        protected abstract void UpdateMovement(GameTime gameTime);

        protected abstract void UpdateExplosion(GameTime gameTime);

        protected abstract void DrawMovement(Camera camera, GraphicsDevice graphics);

        protected abstract void DrawExplosion(Camera camera, GraphicsDevice graphics);

        #endregion

        #region "Methods"
                
        public void Activate()
        {
            this.isUsed = true;
            this.isActive = true;
            this.UpdateDirections();
            this.UpdateWorldMatrix();
        }

        public override void Update(GameTime gameTime)
        {

            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            if (this.isActive)
            {

                this.counter += dt;
                if (this.counter > this.lifespan)
                {
                    this.counter = 0;
                    this.isActive = false;
                }
                else
                {
                    this.UpdateDirections();
                    this.UpdateMovement(gameTime);
                    this.UpdateWorldMatrix();
                }

            }
            else if (this.isExploding)
            {

                this.counter += dt;
                if (this.counter > this.explodingSpan)
                {
                    this.counter = 0;
                    this.isExploding = false;
                    this.isActive = false;
                }
                else
                {
                    this.UpdateDirections();
                    this.UpdateExplosion(gameTime);
                    this.UpdateWorldMatrix();
                }

            }

        }

        public override void Draw(Camera camera, Microsoft.Xna.Framework.Graphics.GraphicsDevice graphics)
        {

            if (!this.IsDestroyed)
            {

                if (this.isActive)
                { 
                    this.DrawMovement(camera, graphics);
                }
                else if (this.isExploding)
                {
                    this.DrawExplosion(camera, graphics);
                }

            }

        }

        public void Hit(Collidable c)
        {

            if (c == null) { return; }

            this.isActive = false;
            this.isExploding = true;

        }

        #endregion

    }
}
